/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package FrameUser;

import Database.CheckConnection;
import FrameSekretariat.SekretariatFrameUtama;
import Model.UserPenerima;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.*;

/**
 *
 * @author WIN7
 */
public class UserLogin extends JFrame {

    private JLabel user;
    private JTextField textUser;
    private JButton buttonOk, buttonCancel;

    public UserLogin() {
        this.setTitle("Login User");
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setBounds(430, 310, 340, 170);
        this.setLayout(null);

        user = new JLabel("User ID");
        user.setBounds(20, 27, 80, 25);
        this.add(user);

        textUser = new JTextField();
        textUser.setBounds(100, 27, 200, 25);
        this.add(textUser);

        buttonOk = new JButton("LOGIN");
        buttonOk.setBounds(70, 78, 80, 30);
        this.add(buttonOk);
        buttonOk.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (textUser.getText().equals("")) {
                    JOptionPane.showMessageDialog(null, "Anda belum memasukan User ID");
                } else {
                    loginUserAction();
                }
            }

            public void loginUserAction() {
                UserPenerima user = new UserPenerima();
                try {
                    user.setIdUserPenerima(textUser.getText());
                    Connection con = CheckConnection.open("jdbc:oracle:thin:@localhost:1521:XE", "hr", "12345");
                    Statement stat = con.createStatement();
                    String sql01 = "select * from USER_PENERIMA where ID_USER = '" + user.getIdUserPenerima() + "'";
                    ResultSet rsql01 = stat.executeQuery(sql01);
                    if (rsql01.next() == true) {
                        String sql = "insert into CACHE(DATA) values('" + user.getIdUserPenerima() + "')";
                        stat.executeUpdate(sql);
                        setVisible(false);
                        UserFrameUtama fu = new UserFrameUtama();
                        fu.setVisible(true);
                    } else {
                        JOptionPane.showMessageDialog(null, "User ID tidak ada");
                    }
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(null, ex.getMessage());
                }
            }
        });

        buttonCancel = new JButton("CANCEL");
        buttonCancel.setBounds(180, 78, 80, 30);
        this.add(buttonCancel);
        buttonCancel.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
            }
        });
    }
}
